package com.carpool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.carpool.R;

import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyStep3Activity extends Activity {
	private Button btnFirstRoute, btnContact, btnFinish;
	private TextView txtCustomAddress;

	ArrayList<String> addresses = new ArrayList<String>();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.step3);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			addresses = bundle.getStringArrayList("addresses");
		}

		btnFirstRoute = (Button)findViewById(R.id.btnFirstRoute);
		btnFirstRoute.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				addresses.add(addresses.get(0));
				Intent intent = new Intent(getApplicationContext(), MyMapActivity.class);
				Bundle bundle = new Bundle();
				bundle.putStringArrayList("addresses", addresses);
				intent.putExtras(bundle);
				startActivity(intent);
				finish();
			}
		});

		btnContact = (Button)findViewById(R.id.btnContact);
		btnContact.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(), MyFullContactActivity.class);
				Bundle bundle = new Bundle();
				bundle.putStringArrayList("addresses", addresses);
				intent.putExtras(bundle);
				startActivity(intent);
				finish();
			}
		});

		txtCustomAddress = (TextView)findViewById(R.id.txtCustomAddress);
		btnFinish = (Button)findViewById(R.id.btnFinish);
		btnFinish.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
				try {
					List<Address> address = geoCoder.getFromLocationName(txtCustomAddress.getText().toString(), 1);
					if (address.size() > 0) 
					{
						addresses.add(address.get(0).getAddressLine(0) + ", " + address.get(0).getAddressLine(1));
						Intent intent = new Intent(getApplicationContext(), MyMapActivity.class);
						Bundle bundle = new Bundle();
						bundle.putStringArrayList("addresses", addresses);
						intent.putExtras(bundle);
						startActivity(intent);
						finish();
					}
					else {
						Toast.makeText(getBaseContext(), "No address could be found.", Toast.LENGTH_LONG).show();
					}
				} catch (IOException e) {
				} catch (NullPointerException e) {
				};
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent){
		super.onActivityResult(requestCode, resultCode, intent);
		Bundle extras = intent.getExtras();

		if (extras != null) {
			addresses.add(extras.getString("address"));
		}

	}
}
